package com.bizfx.component.controls
{	
	import flash.display.DisplayObject;
	
	import com.bizfx.collections.HashMap;
	
	import mx.containers.TitleWindow;
	import mx.containers.VBox;
	import mx.controls.ProgressBar;
	import mx.core.Application;
	import mx.events.ResizeEvent;
	import mx.managers.PopUpManager;

	public class PopUpProgressBar extends ProgressBar {
		
		private var window:TitleWindow;
		
		private var progressBar:PopUpProgressBar;
		
		private var screenMask:VBox;
		
		public function get Text():String {
			if (progressBar) {
				return progressBar.label;
			} else {
				return "";
			}
		}
		public function PopUpProgressBar() {
			super();
			this.visible = false;
			this.includeInLayout = false;
			this.percentWidth = 100;
		}
		
		public function getInstance():PopUpProgressBar {
			if (progressBar == null) {
				progressBar = new PopUpProgressBar();
				progressBar.indeterminate = true;
				progressBar.labelPlacement = "top";
				progressBar.label = "加载中...";
				progressBar.setStyle("trackHeight", 30); 
				progressBar.setStyle("textAlign", "center");
				progressBar.setStyle("paddingTop", 20);
				progressBar.setStyle("paddingLeft", 20);
				progressBar.setStyle("paddingRight", 20);
				progressBar.setStyle("paddingBottom", 20);			
			}
			return progressBar;
		}
		
		private function resetWindow():void {
			if (window) {
				window.showCloseButton = false;
				window.setStyle("headerHeight", 0);
				window.setStyle("verticalAlign", "middle");
				window.minWidth = 350;
			}
		}
		
		private function resetProgressBar():void {
			if (progressBar) {
				progressBar.indeterminate = true;
				progressBar.mode = "manual";
				progressBar.label = "加载中...";
			}
		}
		
		public function show(text:String=null, parent:DisplayObject=null, progress:Number=-1, total:Number=100):void {
			getInstance();
			
			if (!window) {
				this.window = new TitleWindow();
				this.resetWindow();
				this.window.addChild(DisplayObject(progressBar));
			}

			if (text) {
				progressBar.label = text;
			}
			if (progress >= 0) {
				progressBar.indeterminate = false;
				progressBar.setProgress(progress, total);
			} else {
				progressBar.indeterminate = true;
			}
			
			if (!screenMask) {
				screenMask = new VBox();
				screenMask.setStyle("horizontalAlign", "center");
				screenMask.setStyle("verticalAlign", "middle");
				screenMask.setStyle("backgroundColor", "#FFFFFF");
				screenMask.setStyle("backgroundAlpha", "0.4");
				screenMask.horizontalScrollPolicy = "off";
				screenMask.verticalScrollPolicy = "off";
				screenMask.addChild(window);
			}
			
			var p:DisplayObject;
			if (parent) {
				p = parent;
			} else {
				p = DisplayObject(Application.application);
			}
			PopUpManager.addPopUp(screenMask, p, false);
			p.addEventListener(ResizeEvent.RESIZE, maskParentResizeHandler);
			screenMask.height = p.height;
			screenMask.width = p.width;
			
			PopUpManager.centerPopUp(screenMask);
			progressBar.visible = true;
			progressBar.includeInLayout = true;
			window.isPopUp = false;	
		}
		
		public function hide():void {
			if (window) {
				PopUpManager.removePopUp(screenMask);
				progressBar.visible = false;
				progressBar.includeInLayout = false;
				this.resetProgressBar();
			}
		}
		
		private function maskParentResizeHandler(event:ResizeEvent):void {
			var obj:DisplayObject = DisplayObject(event.target);
			screenMask.height = obj.height;
			screenMask.width = obj.width;
		}
	}
}